
local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local RankDefine = require("rank.RankDefine")
local Msg = require("core.Msg")
local RoleFFI  = require("role.RoleFFI")
local RoleDBLogic = require("role.RoleDBLogic")
local PfLogic = require("tencentApi.PfLogic")

LEVEL_RANK = LEVEL_RANK or {}
--LEVEL_RANK = {index = {uuid,lv,name,familyName,create = os.time()},index{uuid,lv,name,familyName}}

function setRankList(human, list, index)
  list.type = RankDefine.LV_RANK_TYPE
  list.name = LEVEL_RANK[index].name
  list.uuid = LEVEL_RANK[index].uuid
  list.lv = LEVEL_RANK[index].lv
  list.familyName = LEVEL_RANK[index].familyName
  list.sex = 0
  if PfLogic.isBlueShow(human) then
    list.blue = LEVEL_RANK[index].blue
  else
    list.blue = "0000"
  end
  list.args = 0
end

function GetRankList(human, sendMsg)
  sendMsg.lvlist[0] = #LEVEL_RANK
  for i=1, #LEVEL_RANK do
    if LEVEL_RANK[i].uuid == human._id then
      sendMsg.myRank = i
    end
    setRankList(human, sendMsg.lvlist[i], i)
  end 
  Msg.send(sendMsg, human.fd)
end

function getFirst(human, list)
  list[0] = 0  
  if LEVEL_RANK[1] then
    list[0] = list[0] + 1
    setRankList(human, list[list[0]], 1)
    local db = RoleDBLogic.getDbByUuid(LEVEL_RANK[1].uuid)
    list[list[0]].sex = db.sex
  end
end

function setRankInfo(db, index)
  LEVEL_RANK[index] = {}
  LEVEL_RANK[index].uuid = RoleFFI.ffi_string(db._id)
  LEVEL_RANK[index].lv = db.lv
  LEVEL_RANK[index].name = RoleFFI.ffi_string(db.name)
  LEVEL_RANK[index].familyName = RoleFFI.ffi_string(db.familyName)
  LEVEL_RANK[index].account = RoleFFI.ffi_string(db.account)
  LEVEL_RANK[index].create = os.time()  
  LEVEL_RANK[index].blue = string.format("%d%d%d%d",db.blueVipLv, db.blueYearVip, db.blueSuperVip, db.level3366)
end

function sortFunc(a, b)
  if a.lv == b.lv then
    return a.create < b.create
  end
  return a.lv > b.lv
end

function isInRank(db)
  for i=1, #LEVEL_RANK do
    if LEVEL_RANK[i].uuid == RoleFFI.ffi_string(db._id) then
      if db.lv ~= LEVEL_RANK[i].lv then 
        setRankInfo(db, i)
        table.sort(LEVEL_RANK, sortFunc)
      end
      return true
    end
  end
  return false
end
function familyChange(db)
  for i=1, #LEVEL_RANK do
    if LEVEL_RANK[i].uuid == RoleFFI.ffi_string(db._id) then
      LEVEL_RANK[i].familyName = RoleFFI.ffi_string(db.familyName)
    end
  end
end
--外部调用，等级变更的时候调用
function lvChange(human, db)
  if not db then
   db = human.db
  end
  if db.lv < 35 then return end
  if isInRank(db) then
    return
  end
  if #LEVEL_RANK < RankDefine.RANK_MAX_CNT then
    setRankInfo(db, #LEVEL_RANK + 1)
  elseif #LEVEL_RANK >= RankDefine.RANK_MAX_CNT and db.lv > LEVEL_RANK[#LEVEL_RANK].lv then
    setRankInfo(db, RankDefine.RANK_MAX_CNT)
  else
    return false
  end  
  table.sort(LEVEL_RANK, sortFunc)
end

function getRank()
  local dbType = {type = RankDefine.LV_RANK_TYPE}
  LuaMongo.find(DB.db_rank, dbType)
  while true do
    local tmp = {}
    if not LuaMongo.next(tmp) then
      break
    end
    LEVEL_RANK[tmp.index] = {}
    LEVEL_RANK[tmp.index].uuid = tmp.uuid
    LEVEL_RANK[tmp.index].lv = tmp.lv
    LEVEL_RANK[tmp.index].name = tmp.name
    LEVEL_RANK[tmp.index].familyName = tmp.familyName
    LEVEL_RANK[tmp.index].account = tmp.account
    LEVEL_RANK[tmp.index].create = tmp.create or os.time()
    LEVEL_RANK[tmp.index].blue = tmp.blue or "0000"
  end
end

function wrapperLvData(index)
  local db = {}
  db.type = RankDefine.LV_RANK_TYPE
  db.index = index
  db.uuid = LEVEL_RANK[index].uuid
  db.lv = LEVEL_RANK[index].lv
  db.name = LEVEL_RANK[index].name
  db.familyName = LEVEL_RANK[index].familyName
  db.account = LEVEL_RANK[index].account
  db.create = LEVEL_RANK[index].create or os.time()
  db.blue = LEVEL_RANK[index].blue or "0000"
  return db
end

function saveRank()
  for i=1, #LEVEL_RANK do
    local db = wrapperLvData(i)
    LuaMongo.insert(DB.db_rank, db)
  end
end